package com.bioscript.standalone.dataStructures;

import org.w3c.dom.Document;
import org.w3c.dom.Element;


public final class BlastHit {

	private Range query, source;
	private double eVal = 0;
	private double bitScore = 0;
	private int alignmentLength = 0;
	private int mismatches = 0;
	private double identity = 0;
	private int gapOpenings = 0;
	
	public static BlastHit getNewInstance(String hit)
	{
		String[] params = hit.split("\t");
		Range sourceRange = new Range(Integer.parseInt(params[8]), 
				Integer.parseInt(params[9]), params[1]);
		Range query = new Range(Integer.parseInt(params[6]), Integer.parseInt(params[7]),  params[0]);
		return new BlastHit(query, sourceRange).
		setAlignmentLength(Integer.parseInt(params[3])).
		setIdentity(Double.parseDouble(params[2])).
		setBitScore(Double.parseDouble(params[11])).
		setMismatches(Integer.parseInt(params[4])).
		seteVal(Double.parseDouble(params[10])).
		setgapOpenings(Integer.parseInt(params[5]));
	}
	
	public BlastHit(Range query, Range source) 
	{
		this.query = new Range(query.getLowerLimit(), query.getUpperLimit(), 
				query.getName()).setMetadata(query.getMetadata());
		this.source = new Range(source.getLowerLimit(), 
				source.getUpperLimit(), source.getName()).setMetadata(source.getMetadata());
	}
	
	public Range getQuery()
	{
		return new Range(query.getLowerLimit(), query.getUpperLimit(), 
				query.getName()).setMetadata(query.getMetadata());
	}
	
	public Range getSource()
	{
		return new Range(source.getLowerLimit(), source.getUpperLimit(), 
				source.getName()).setMetadata(source.getMetadata());
	}
	
	public double geteVal() {
		return eVal;
	}

	public double getBitScore() {
		return bitScore;
	}

	public int getAlignmentLength() {
		return alignmentLength;
	}

	public int getMismatches() {
		return mismatches;
	}

	public double getIdentity() {
		return identity;
	}
	
	public double getGapOpenings() {
		return gapOpenings;
	}
	
	public BlastHit setgapOpenings(int gapOpenings) {
		this.gapOpenings = gapOpenings;
		return this;
	}
	
	public BlastHit seteVal(double eVal) {
		this.eVal = eVal;
		return this;
	}

	public BlastHit setBitScore(double bitScore) {
		this.bitScore = bitScore;
		return this;
	}

	public BlastHit setAlignmentLength(int alignmentLength) {
		this.alignmentLength = alignmentLength;
		return this;
	}

	public BlastHit setMismatches(int mismatches) {
		this.mismatches = mismatches;
		return this;
	}

	public BlastHit setIdentity(double identity) {
		this.identity = identity;
		return this;
	}
	
	public Element toXML(Document doc)
	{
		Element hit = doc.createElement("hit");
		hit.setAttribute("sourceStart", String.valueOf(source.getLowerLimit()));
		hit.setAttribute("sourceEnd", String.valueOf(source.getUpperLimit()));
		hit.setAttribute("eVal", String.valueOf(eVal));
		hit.setAttribute("bitScore", String.valueOf(bitScore));
		hit.setAttribute("alignmentLength", String.valueOf(alignmentLength));
		hit.setAttribute("mismatches", String.valueOf(mismatches));
		hit.setAttribute("sourceName", source.getName());
		hit.setAttribute("identity", String.valueOf(identity));
		hit.setAttribute("gapOpenings", String.valueOf(gapOpenings));
		return hit;
	}
}
